<?php 
require_once (dirname(__FILE__)."/services.php");
class glossary_RouterExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['rule']['{^/glossary/.*$}'] = dirname(__FILE__)."/actions.php";
        $chain->next($context);
    }
}
class glossary_getAllExtension extends Extension {
	public function apply(&$context, Chain $chain) {
		
		$context = service_glossary_getAll(0, 10000);
		
        $chain->next($context);
    }
}
class glossary_getTermByGlossaryExtension extends Extension {
	public function apply(&$context, Chain $chain) {
		
		foreach($context['sources'] as $glossary_serial) {
			$context['terms'][$glossary_serial] = service_glossary_getTerms($glossary_serial, "", 0, 10000);
		}
		
        $chain->next($context);
    }
}
class glossary_getNameBySerialExtension extends Extension {
	public function apply(&$context, Chain $chain) {
		$sources_all = service_glossary_getAll(0, 10000);
		$sources_keyable = array();
		foreach($sources_all as $source) {
			$sources_keyable[$source['serial']] = $source;
		}
		$context['sources'] = $sources_keyable;
        $chain->next($context);
    }
}
class glossary_getURLBySerialExtension extends Extension {
	public function apply(&$context, Chain $chain) {
		$url = array();
		$context['url'] = REQUEST_BASE.'glossary/'.$context['serial'].'/show/';
        $chain->next($context);
    }
}

class glossary_PrivilegeExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['items']['glossary']['text'] = "Glossary";
        $context['items']['glossary']['items']['edit']['text'] = "Edit the glossary";
        $chain->next($context);
    }
}
class glossary_InterceptorExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['rule']['{^/glossary/.*$}'][] = new glossary_LoginInterceptor();
        $context['rule']['{^/glossary/new/.*$}'][] = new glossary_EditorInterceptor();
        $context['rule']['{^/glossary/([^/]+)/edit/$}'][] = new glossary_EditorInterceptor();
        $context['rule']['{^/glossary/([^/]+)/addterms/$}'][] = new glossary_EditorInterceptor();
        $context['rule']['{^/glossary/editterm/$}'][] = new glossary_EditorInterceptor();
        $context['rule']['{^/glossary/test/$}'][] = new glossary_EditorInterceptor();
        $context['rule']['{^/glossary/deleteterm/$}'][] = new glossary_EditorInterceptor();
        $context['rule']['{^/glossary/delete/$}'][] = new glossary_EditorInterceptor();
        $chain->next($context);
    }
}
class glossary_LoginInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
            $chain->next($context);
        } else {
            $mvc = new MVC(array());
            $mvc->setMessage("message", "Please login first!");
            $currentURI = Utils::getURI();
            $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
        }
    }
}
class glossary_EditorInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
            $userContext = array("user"=>$currentUser['serial']);
            $featureManager->apply("com.yizin.platform.role.get.by.user", $userContext);
            $roleContext = array('role'=>$userContext['role']['serial'], 'id'=>'glossary.edit');
            $featureManager->apply("com.yizin.platform.privilege.get.by.id", $roleContext);
            if (!($roleContext['allow'])) {
                $mvc = new MVC(array());
                $mvc->setMessage("message", "Permisson Deny.");
                $currentURI = Utils::getURI();
                $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
            };
        }
        $chain->next($context);
    }
}

class glossaryFeature extends Feature {
    public function registerExtension() {
        $this->extensions = Array( 
              	 new glossary_RouterExtension("com.yizin.platform.router"),
				 new glossary_getAllExtension("dictionary.get.all.glossaries"),
				 new glossary_getTermByGlossaryExtension("dictionary.get.term.by.source"),
				 new glossary_getNameBySerialExtension("dictionary.get.source.name.by.serial"),
				 new glossary_getURLBySerialExtension("dictionary.get.source.url.by.serial"),
				 new glossary_InterceptorExtension("com.yizin.platform.interceptor"),
                 new glossary_PrivilegeExtension("com.yizin.platform.privilege"),
		);
    }
    
    public function registerExtendable() {
    }
    
    public function init() {
    	$conn = getDBConnection();
		$conn->query('drop table if exists glossaries;');
		$conn->query('drop table if exists glossary_terms;');
    	$conn->query('
		create table glossaries
		(
			serial bigint primary key auto_increment,
			title varchar(255) not null,
			abbr varchar(255),
			author varchar(255),
			editor varchar(255),
			version varchar(255),
			publisher varchar(255),
			year_published varchar(255),
			source text,
			description text,
			creator bigint not null,
			create_date datetime not null,
			last_editor bigint null,
			last_edit_date datetime null
		) CHARSET=utf8;
		');
		$conn->query('
		create table glossary_terms
		(
			serial bigint primary key auto_increment,
			term varchar(255) not null,
			definition text not null,
			word_class varchar(255) null,
			national_variants varchar(255) null,
			admitted_terms text null,
			deprecated_terms text null,
			abbreviation varchar(255) null,
			acronym varchar(255) null,
			full_forms text null,
			source_location text null,
			subject_field text null,
			broader_term varchar(255) null,
			narrower_term varchar(255) null,
			synonym varchar(255) null,
			name_of_creators text null,
			notes text null,
			creator bigint not null,
			create_date datetime not null,
			last_editor bigint null,
			last_edit_date datetime null,
			belongto bigint not null,
			version int not null
		) CHARSET=utf8;
		');
		$conn->disconnect();
	}
    
    public function destroy() {
    	$conn=getDBConnection();
		$conn->query('drop table if exists glossaries;');
		$conn->query('drop table if exists glossary_terms;');
		$conn->disconnect();
    }
}

$featureClass = "glossaryFeature";
$featureName = "Glossary";
$featureIntroduction = "glossary feature";
?>
